<?php
/**
 * Mailer class
 *
 * @author Nikita
 *
 */
class Lemar_Mailer
{
	const ADMIN_EMAIL = 'dima@dev.com';
	/**
	 * Send mail to site
	 *
	 * @param string $sBody - message text body
	 * @param string $sFromAddress - address from
	 * @param string $sFromName - name from
	 * @param string $sSubject - subject
	 *
	 * @return bool
	 */
	public static function sendToSite($sBody, $sFromAddress, $sFromName, $sSubject)
	{
		$sMail = 'email';
        $oConfig = Zend_Registry::getInstance()->get('config')->$sMail;

		$sToAddress = (null !== $oConfig->toAddress) ? $oConfig->toAddress : '';
		$sToName = (null !== $oConfig->toName) ? $oConfig->toName : '';

		return self::send($sBody, $sToAddress, $sToName, $sFromAddress, $sFromName, $sSubject);
	}
	
	public static function sendToAdmin($sBody, $sFromAddress, $sFromName, $sSubject)
	{
//		$aoUser =  models_UserMapper::fetchByField('role', 'administrator');
//		//		print_r($aoUser); die;
//		$sToAddress = (isset($aoUser[0]->email)) ? $aoUser[0]->email:'';
//		$sToName = (isset($aoUser[0]->name)) ? $aoUser[0]->name : '';
		$sToAddress = self::ADMIN_EMAIL;
		$sToName	= '';
		return self::send($sBody, $sToAddress, $sToName, $sFromAddress, $sFromName, $sSubject);
	}	

	/**
	 * Send mail from site
	 *
	 * @param string $sBody - message text body
	 * @param string $sToAddress - address to
	 * @param string $sToName - name to
	 * @param string $sSubject - subject
	 *
	 * @return bool
	 */
	public static function sendFromSite($sBody, $sToAddress, $sToName, $sSubject)
	{
//		$sMail = 'email';
//        $oConfig = Zend_Registry::getInstance()->get('config')->$sMail;
//		print_r($oConfig); die;
//		$sFromAddress = (null !== $oConfig->fromAddress) ? $oConfig->fromAddress : '';
//		$sFromName = (null !== $oConfig->fromName) ? $oConfig->fromName : '';
		$sFromAddress = self::ADMIN_EMAIL;
		$sFromName = 'admin';
		
		return self::send($sBody, $sToAddress, $sToName, $sFromAddress, $sFromName, $sSubject);
	}

	/**
	 * Send mail
	 *
	 * @param string $sBody - message text body
	 * @param string $sToAddress - address to
	 * @param string $sToName - name to
	 * @param string $sFromAddress - address from
	 * @param string $sFromName - name from
	 * @param string $sSubject - subject
	 *
	 * @return bool
	 */
	public static function send($sBody, $sToAddress, $sToName, $sFromAddress, $sFromName, $sSubject)
	{
		/* @todo use SMTP server */
		$oMail = new Zend_Mail();
		$oMail->setBodyText($sBody);
		$oMail->setFrom($sFromAddress, $sFromName);
		$oMail->addTo($sToAddress, $sToName);
		$oMail->setSubject($sSubject);

		if($oMail->send())
		{
			return true;
		}
		else
		{
			Lemar_Logger::log('Lemar_Mailer::send: error  Zend_Mail->send() ', $iPriority = Zend_Log::ERR);
		}
		return false;
	}

}
